package org.szy.monitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.auth.util.SaLoginUtil;
import org.szy.core.common.cache.CacheOperator;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.monitor.param.CacheParam;

import java.util.List;
import java.util.Map;

/**
 * 监控-缓存管理控制器
 *
 * @author 孙志阳
 * @date 2023-07-05
 * @since 1.0.0
 **/
@Controller
@Mapping("/monitor/cache")
public class CacheController extends BaseController {

    private static String PATH_PREFIX = "monitor/cache/";
    @Inject
    private CacheOperator cacheOperator;

    @Get
    @Mapping("/index")
    @SaCheckPermission("monitor_cache")
    public ModelAndView index() {
        return new ModelAndView(PATH_PREFIX + "index");
    }

    @Get
    @Mapping("/list")
    @SaCheckPermission("monitor_cache_list")
    public R<?> list() {
        List<Map<String, Object>> list = cacheOperator.listAllKeyValues();
        return R.data(list);
    }

    @Delete
    @Mapping("/delete")
    @SaCheckPermission("monitor_cache_delete")
    public R<?> delete(String keys) {
        Long aLong = cacheOperator.remove(keys.split(","));
        return R.data(aLong);
    }

    @Put
    @Mapping("/update")
    @SaCheckPermission("monitor_cache_update")
    public R<?> save(@Validated CacheParam param) {
        cacheOperator.put(param.getKey(), param.getValue());
        return R.ok();
    }

}
